package com.scu.qingshi.infrastructure.utils;

import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author qingshi
 */
@Component
public class VerifyCodeUtil {

    private final Random random = new Random();
    private static final String CHARACTER = "0123456789abcdefGhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 用于构建基础验证码
     *
     * @param length           验证码长度
     * @param containCharacter 验证码是否包含字母
     */
    public String getVerify(int length, boolean containCharacter) {
        StringBuilder result = new StringBuilder();
        if (containCharacter) {
            while (length > 0) {
                result.append(CHARACTER.charAt(random.nextInt(CHARACTER.length())));
                length--;
            }
        } else {
            while (length > 0) {
                result.append(random.nextInt(10));
                length--;
            }
        }

        return result.toString();
    }
}